// 云存储文件管理API服务
// REF: API-CLOUD-FILES-010

import { apiClient, ApiResponse } from '@/lib/api';

// 云存储文件信息接口
export interface CloudFile {
  id: string;
  name: string;
  path: string;
  parentId: string;
  size: number;
  type: 'file' | 'folder';
  isFolder: boolean;
  createTime: string;
  modifyTime: string;
  modifiedTime: string;
  md5?: string;
  sha1?: string;
  downloadUrl?: string;
  thumbnailUrl?: string;
  mimeType?: string;
  extension?: string;
}

// 文件列表请求参数
export interface GetFilesRequest {
  accountId: number;
  folderId?: string;
  pageNum?: number;
  pageSize?: number;
  foldersOnly?: boolean;
  autoLogin?: boolean;
}

// 文件列表响应
export interface GetFilesResponse {
  files: CloudFile[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

// 创建文件夹请求参数
export interface CreateFolderRequest {
  accountId: number;
  name: string;
  parentPath?: string;
}

// 删除文件请求参数
export interface DeleteFilesRequest {
  accountId: number;
  paths: string[];
}

// 移动/重命名文件请求参数
export interface MoveFileRequest {
  accountId: number;
  sourcePath: string;
  targetPath: string;
  newName?: string;
}

// 复制文件请求参数
export interface CopyFileRequest {
  accountId: number;
  sourcePath: string;
  targetPath: string;
  newName?: string;
}

// 获取文件详情请求参数
export interface GetFileInfoRequest {
  accountId: number;
  filePath: string;
}

// 获取下载链接请求参数
export interface GetDownloadUrlRequest {
  accountId: number;
  filePath: string;
}

// 获取下载链接响应
export interface GetDownloadUrlResponse {
  downloadUrl: string;
  expiresAt?: string;
}

class CloudFilesApi {
  private baseUrl = '/cloud-files';

  /**
   * 获取文件列表
   */
  async getFiles(params: GetFilesRequest): Promise<ApiResponse<GetFilesResponse>> {
    const response = await apiClient.get(`${this.baseUrl}/files`, {
      params: {
        accountId: params.accountId,
        folderId: params.folderId,
        pageNum: params.pageNum,
        pageSize: params.pageSize,
        foldersOnly: params.foldersOnly,
        autoLogin: params.autoLogin
      }
    });
    return response.data;
  }

  /**
   * 检测登录状态
   */
  async checkLoginStatus(accountId: number): Promise<ApiResponse<{ isLoggedIn: boolean }>> {
    const response = await apiClient.get(`${this.baseUrl}/login-status`, {
      params: { accountId }
    });
    return response.data;
  }

  /**
   * 手动登录
   */
  async login(accountId: number): Promise<ApiResponse<{ success: boolean }>> {
    const response = await apiClient.post(`${this.baseUrl}/login`, {
      accountId
    });
    return response.data;
  }

  /**
   * 创建文件夹
   */
  async createFolder(data: CreateFolderRequest): Promise<ApiResponse<CloudFile>> {
    const response = await apiClient.post(`${this.baseUrl}/folders`, data);
    return response.data;
  }

  /**
   * 删除文件或文件夹
   */
  async deleteFiles(data: DeleteFilesRequest): Promise<ApiResponse<void>> {
    const response = await apiClient.delete(`${this.baseUrl}/files/delete`, { data });
    return response.data;
  }

  /**
   * 移动/重命名文件或文件夹
   */
  async moveFile(data: MoveFileRequest): Promise<ApiResponse<CloudFile>> {
    const response = await apiClient.put(`${this.baseUrl}/files/move`, data);
    return response.data;
  }

  /**
   * 复制文件或文件夹
   */
  async copyFile(data: CopyFileRequest): Promise<ApiResponse<CloudFile>> {
    const response = await apiClient.post(`${this.baseUrl}/files/copy`, data);
    return response.data;
  }

  /**
   * 上传文件
   */
  async uploadFile(formData: FormData): Promise<ApiResponse<CloudFile>> {
    const response = await apiClient.post(`${this.baseUrl}/upload`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
    return response.data;
  }

  /**
   * 获取文件详细信息
   */
  async getFileInfo(params: GetFileInfoRequest): Promise<ApiResponse<CloudFile>> {
    const response = await apiClient.get(`${this.baseUrl}/files/info`, {
      params: {
        accountId: params.accountId,
        filePath: params.filePath
      }
    });
    return response.data;
  }

  /**
   * 获取文件下载链接
   */
  async getDownloadUrl(params: GetDownloadUrlRequest): Promise<ApiResponse<GetDownloadUrlResponse>> {
    const response = await apiClient.get(`${this.baseUrl}/download-url`, {
      params: {
        accountId: params.accountId,
        filePath: params.filePath
      }
    });
    return response.data;
  }
}

export const cloudFilesApi = new CloudFilesApi();
export default cloudFilesApi;